<?php

$chat_room_mem_key = "chat_rooms";
$chat_room_cache_long = 120 ;

function check_user_rooms($user_id){
	global $chat_room_mem_key, $chat_room_cache_long ;
	$matrix = array();
	$rooms = mem_get($chat_room_mem_key,$matrix);
	if(is_array($rooms)){
		$values = array_values($rooms);
		$matrix = array();
		for($i=0;$i<count($values);$i++){
			$room = $values[$i];
			$user = $room->find_user_by_id($user_id);
			if($user != false){
				array_push($matrix,$room);
			}
		}
	}
	return $matrix ;
}

function regist_chatroom($room){
	global $chat_room_mem_key, $chat_room_cache_long ;
	$matrix = array();
	$rooms = mem_get($chat_room_mem_key,$matrix);
	$rooms[$room->id] = $room ;
	mem_replace($chat_room_mem_key,$rooms,0,$chat_room_cache_long);
}

function release_chatroom($id){
	global $chat_room_mem_key, $chat_room_cache_long ;
	$rooms = mem_get($chat_room_mem_key);
	if($rooms != false && array_key_exists($id,$rooms)){
		unset($rooms[$id]);
		mem_cache($chat_room_mem_key, $room, 0, $chat_room_cache_long);
	}
}

function chat_room_exists($id){
	global $chat_room_mem_key ;
	$rooms = mem_get($chat_room_mem_key);
	return $rooms != false && array_key_exists($id, $rooms) ;
}

function get_chat_room($id,$name="",$create_user=""){
	if(chat_room_exists($id)){
		global $chat_room_mem_key ;
		$rooms = mem_get($chat_room_mem_key);
		return $rooms[$id];
	}
	$room = new ChatRoom($id,$name,$create_user);
	regist_chatroom($room);
	return $room ;
}

function add_user_to_room($room_id,$user_id,$user_name,$gender=0){
	$file = get_chatroom_file($room_id) ;
	$lock = new Lock($file,$type=0,$timeout=5000,$interval=50);
	$lock->wait_lock();
	$room = get_chat_room($room_id,"");
	$user = new ChatUser($user_id,$user_name,$gender) ;
	$room->add_user($user);
	regist_chatroom($room);
	$lock->release_lock();
}

function get_room_message($room_id,$user_id){
	$file = get_chatroom_file($room_id) ;
	$lock = new Lock($file,$type=0,$timeout=5000,$interval=50);
	$lock->wait_lock();
	$room = get_chat_room($room_id,"");
	$list = $room->fetch_message($user_id);
	regist_chatroom($room);
	$lock->release_lock();
	return $list ;
}

function send_message($room_id,$room_name,$user_id,$user_name,$font_style,$content){
	$file = get_chatroom_file($room_id) ;
	$lock = new Lock($file,$type=0,$timeout=5000,$interval=50);
	$lock->wait_lock();
	$room = get_chat_room($room_id,$room_name);
	$user = new ChatUser($user_id,$user_name);
	$msg = new ChatMessage($user_id,$user_name,$font_style,$content);
	$room->user_login($user);
	$room->add_message($msg);
	regist_chatroom($room);
	$lock->release_lock();
}

function get_chatroom_file($room_id){
	global $configs ;
	return $configs["temp_file_dir"] . "/$room_id" . ".lock" ;
}

class ChatUser{
	var $id ;
	var $name ;
	var $gender ;
	var $login_time = 0 ;
	var $last_fetch_time = 0 ;
	
	function ChatUser($id,$name,$gender=0){
		$this->id = $id ;
		$this->name = $name ;
		$this->gender = $gender ;
	}

	function note_login(){
		$this->login_time = time();
	}
}

class ChatMessage{
	var $font_style ;
	var $content ;
	var $user_id ;
	var $user_name ;
	var $create_time ;

	function ChatMessage($user_id,$user_name,$font_style,$content){
		$this->user_id = $user_id ;
		$this->user_name = $user_name ;
		$this->font_style = $font_style ;
		$this->content = $content ;
		$this->create_time = time() ;
	}
}

class ChatRoom{
	var $id ;
	var $name ;
	var $last_active_time ;
	var $users = array();
	var $messages = array();
	var $attributes = array();
	var $create_user = null ;

	function ChatRoom($id,$name="",$create_user=""){
		$this->id = $id ;
		$this->name = $name ;
		$this->last_active_time = time()  ;
		$this->create_user = $create_user ;
	}

	function add_user($user){
		if($this->find_user_by_id($user->id) != false){
			return ;
		}
		$this->users[$user->id] = $user ;
	}

	function find_user_by_id($user_id){
		if(array_key_exists($user_id,$this->users)){
			return $this->users[$user_id] ;
		}
		return false ;
	}

	function user_login($user){
		$id = $user->id ;
		if($this->find_user_by_id($id) == false){
			$this->add_user($user);
		}
		$this->users[$id]->login_time = time() ;
	}

	function user_logout($user_id){
		if(array_key_exists($user_id,$this->users)){
			unset($this->users[$user_id]) ;
		}
	}

	function user_absent($userId){
		if(!array_key_exists($userId,$this->users)){
			return false ;
		}
		$user = $this->users[$userId];
		return $user->login_time <= 0 ;
	}

	function add_message($msg){
		array_push($this->messages , $msg) ;
	}

	function fetch_message($user_id){
		$user = $this->find_user_by_id($user_id);
		if($user == false){
			return false ;
		}
		$last_fetch_time = $user->last_fetch_time ;
		$this->users[$user_id]->last_fetch_time = time() ;
		$this->last_active_time = time() ;
		$size = count($this->messages);
		$list = array();
		for($i=($size-1);$i>=0;$i--){
			$msg = $this->messages[$i];
			if($msg->create_time >= $last_fetch_time){
				array_push($list,$msg);
			}else{
				break ;
			}
		}
		return $list ;
	}

	function get_messages(){
		return $this->messages ;
	}

	function is_invalid(){
		return count($this->users) == 0 || (time() - $this->last_active_time) > 1800  ; 
	}

	function close(){
		release_chatroom($this->id);
	}
}

?>